题目地址 (opens new window)

  • 🙂 第一次练习 2020-05-31 题解 (opens new window) 用到了剪支的技巧。这个递归自己也没想到。大概知道用高度来做。
  • 😄 第二次练习

# 解题方法

解题代码

class Solution {
    boolean isBalance = true;

    public boolean isBalanced(TreeNode root) {
        if (root == null) {
            return true;
        }

        getDepth(root);
        return isBalance;
    }

    public int getDepth(TreeNode root) {ßß
        if (!isBalance) {
            return 0;
        }
        if (root == null) {
            return 0;
        }

        int left = getDepth(root.left);
        int right = getDepth(root.right);

        if (Math.abs(left - right) > 1) {
            isBalance = false;
        }
        return Math.max(left, right) + 1;
    }
}

# 易错点

  • 易错项 1
最后编辑时间: 7/14/2020, 9:21:47 AM